package com.example.work12.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.work12.MainActivity;
import com.example.work12.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class WelcomeFragment extends Fragment {
    private TextView textView;
    private Button button;
    private boolean flag = false;
    private static final String TAG = "WelcomeFragment";
    private Handler handler = new Handler();
    public WelcomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_welcome, container, false);
        textView = view.findViewById(R.id.texts);
        button = view.findViewById(R.id.tian);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int i = 5;
            @Override
            public void run() {
                if(!flag){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText("倒计时:"+i--);
                        }
                    });
                    if(i == 0){
                        Log.i(TAG, "run: "+i);
                        Intent intent = new Intent(getContext(), MainActivity.class);
                        startActivity(intent);
                        getActivity().finish();
                        timer.cancel();
                    }
                }
            }
        },0,1000);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flag = true;
                Intent intent = new Intent(getContext(), MainActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }

}
